<?php
namespace app\cdyoa\controller;
use think\Db;
class Outbx extends Base
{

    public function lists(){
        $kemu = \app\cdyoa\model\Kemu::lists();
        if( is_post() ){
            $page     = input('page');
            $keyword  = input('keyword');
            $status  = input('status');
            $min_time = input('min_time');
            $max_time = input('max_time');

            $sql = "
                SELECT
                    [1]
                FROM
                    out_bx a
                left join
                    admin_user b
                on
                    a.user_id = b.id
                left join
                    admin_user c
                on
                    a.one_user_id = c.id
                left join
                    admin_user d
                on
                    a.two_user_id = d.id
                left join
                    orders e
                on
                    a.orders_id=e.id
                WHERE
                    1 = ?
                ";
            $param = [1];
            if ( !empty($keyword) ) {
                $sql .= " and (e.order_no like ? or e.title like ? or e.contract_no like ? or a.content like ? a.name like ?)";
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
            }
            if ( $status != '100all' ) {
                switch ($status){
                    case 0:
                        $sql .= " and (a.one_user_status = ? and a.two_user_status = ?)";
                        $param[] = $status;
                        $param[] = $status;
                        break;
                    case 1:
                        $sql .= " and (a.one_user_status = ? and a.two_user_status = ?)";
                        $param[] = $status;
                        $param[] = $status;
                        break;
                    case 2:
                        $sql .= " and (a.one_user_status = ? or a.two_user_status = ?)";
                        $param[] = $status;
                        $param[] = $status;
                        break;
                }
            }
            if ( !empty($min_time) ) {
                $sql    .= " and a.create_time >= ?";
                $param[] = strtotime($min_time);
            }
            if ( !empty($max_time) ) {
                $sql    .= " and a.create_time <= ?";
                $param[] = strtotime($max_time);
            }
            //分页
            $sql_count = str_replace("[1]", "count(*) as total", $sql);
            $total     = Db::query($sql_count, $param);
            $total     = $total[0]['total'];
            $page_size   = c('page_size');
            $page        = com_cur_page($total, $page,  $page_size);
            $page_data   = com_page_data($total, $page,  $page_size);

            //列表
            $start       = ($page - 1) * $page_size;
            $sql        .= " order by a.id desc limit $start, $page_size";
            $field       = "a.*,b.name as lname,e.order_no,e.title,e.contract_no";
            $sql_list    = str_replace("[1]", $field, $sql);
            $list        = Db::query($sql_list, $param);
            //格式数据
            foreach($list as $k => $v){
                $list[$k]['type']           = $kemu[$v['type']];
                $list[$k]['one_user_status']           =  c('type.outbx_status',$v['one_user_status']);
                $list[$k]['two_user_status']           =  c('type.outbx_status',$v['two_user_status']);

                #经理
                $list[$k]['one_user_status']           =  c('type.outbx_status',$v['one_user_status']);
                switch ($v['one_user_status']){
                    case 0:
                        $list[$k]['one_user_status'] = '<span class="label label-default radius">'.$list[$k]['one_user_status'].'</span>';
                        break;
                    case 1:
                        $list[$k]['one_user_status'] = '<span class="label label-success radius">'.$list[$k]['one_user_status'].'</span>';
                        break;
                    case 2:
                        $list[$k]['one_user_status'] = '<span class="label label-warning radius">'.$list[$k]['one_user_status'].'</span>';
                        break;
                }

                #财务
                $list[$k]['two_user_status']           =  c('type.outbx_status',$v['two_user_status']);
                switch ($v['two_user_status']){
                    case 0:
                        $list[$k]['two_user_status'] = '<span class="label label-default radius">'.$list[$k]['two_user_status'].'</span>';
                        break;
                    case 1:
                        $list[$k]['two_user_status'] = '<span class="label label-success radius">'.$list[$k]['two_user_status'].'</span>';
                        break;
                    case 2:
                        $list[$k]['two_user_status'] = '<span class="label label-warning radius">'.$list[$k]['two_user_status'].'</span>';
                        break;
                }
                $list[$k]['money']           =  number_format($v['money'],2);
                $list[$k]['create_time']    =  com_format_time( $v['create_time'] );
                //操作按钮
                $list[$k]['action'] = com_page(['ico'=>'yanjing','title'=>'查看报销','url'=>url('outbx/detail','id='.$v['id'])]);
                if( ($v['one_user_status']==0&&$v['two_user_status']==0) || $v['one_user_status']==2 || $v['two_user_status']==2 ){
                    $list[$k]['action'].=com_page(['ico'=>'del3','title'=>'删除','url'=>url('c/del'), 'post'=>['id'=>$v['id'],'t'=>'out_bx'],'confirm'=>"你确定要删除吗"]);
                }
            }
            $com_body    = com_build_table($list, array("id","lname", "order_no" ,"title","contract_no" ,"name","type","money","one_user_status","one_user_note","two_user_status","two_user_note", "create_time","action"));
            $com_page    = com_build_page( $page_data );
            $data = array(
                "com_body"  => $com_body,
                "com_page"  => $com_page,
            );
            com_check(['data'=>$data]);
        }
        $data = [
            'nav'       => ['报销列表', '列表'],
            'status'    =>  ['100all'=>'全部状态'] + c('type.outbx_status'),

        ];
        return view('', $data);
    }


    //添加报销
    public function add($id=''){
        if( is_post() ){
            $data = com_data(input(), ['id']);
            $rule = [
                'name'      =>  'require',
                'type'      =>  'require|float',
                'money'        =>  'require|float',
            ];
            $msg = [];
            com_validate( $rule, $msg, $data, true );
            $data['orders_id']   = $id;
            $data['user_id']     = $this->userinfo['id'];
            $data['create_time'] = time();
            Db::name('out_bx')->insert($data);
            $config = [
                'msg' => '添加成功！',
                'suc' => [
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        }

        $kemu = \app\cdyoa\model\Kemu::lists();
        $d = [
            'info' => Db::name('orders')->find($id),
            'type' => $kemu,
            'id' => $id,
        ];
        return view('',$d);
    }

    public function check(){
        if(is_post()){
            $f = input('f');
            if($f=='one'){
                $d = [
                    'id' => input('id'),
                    'one_user_id' => $this->userinfo['id'],
                    'one_user_status' => input('status'),
                    'one_user_note' => input('pass'),
                    'update_time'   => time(),
                ];
                Db::name('out_bx')->update($d);
            }else{
                $d = [
                    'id' => input('id'),
                    'two_user_id' => $this->userinfo['id'],
                    'two_user_status' => input('status'),
                    'two_user_note' => input('pass'),
                    'update_time'   => time(),
                ];
                Db::name('out_bx')->update($d);
            }
            $config = [
                'suc' => [
                    'msg' => '操作成功！',
                    'js_action'=>"alert|reload",
                ],
            ];
            com_check( $config );
        }
    }

    //详情
    public function detail($id=''){
        $sql = "
                SELECT
                    a.*,b.name as lname,e.order_no,e.title,e.contract_no
                FROM
                    out_bx a
                left join
                    admin_user b
                on
                    a.user_id = b.id
                left join
                    admin_user c
                on
                    a.one_user_id = c.id
                left join
                    admin_user d
                on
                    a.two_user_id = d.id
                left join
                    orders e
                on
                    a.orders_id=e.id
                WHERE
                    a.id = ? limit 1
                ";
        $rs = Db::query($sql,[$id]);
        $info = $rs[0];
        $data = [
            'info' => $info,
        ];
        return view('', $data);
    }
}
